
#include "setting_page.h"
#include "setting_page2.h"
#include "pt_volume.h"
#include "pt_bright.h"
#include "sys_font.h"
#include "pt_fs.h"
#include "setting_msg.h"
#include "root.h"

static  ui_widget_setting_t  ui_set_p;

static  char* icon_point_setting[2] =
{
    "Dot_Default.png",
    "Dot_Selected.png",
};

static  char* icon_msg[2] =
{
    "News_Default.png",
    "News_Selected.png",
};

static  char* icon_voice[2] =
{
    "Voice_Default.png",
    "Voice_Selected.png",
};

static  char* icon_sleep[2] =
{
    "Sleep_Default.png",
    "Sleep_Selected.png",
};

static  char* icon_bri[2] =
{
    "AutoBrightness_Default.png",
    "AutoBrightness_Selected.png",
};

static  char* icon_volume[1] =
{
    "Volume_Disabled.png",
};

static  char* icon_bright[1] =
{
    "Brightness_Disabled.png",
};


static int setting_msg_clicked_event(void)
{
    ui_set_p.is_msg = !ui_set_p.is_msg;

    lv_img_set_src(ui_set_p.img_msg, pt_fs_get(icon_msg[ui_set_p.is_msg]));

    root_gesture_disable();

    page_install("setting_msg", lv_page_setting_msg_create(),NULL);
    page_push_anim("setting_msg",LOAD_ANIM_MOVE_LEFT);


    return 0;
}

static void setting_msg_event_callback(lv_event_t* event)
{
    lv_point_t  p;
    lv_indev_t* indev  = NULL;
    indev = lv_indev_get_act();
    if(!indev){
        return -1;
    }

    lv_indev_get_point(indev, &p);

    switch(event->code){
    case LV_EVENT_CLICKED:
    {
        debug_d("c=%d:%d\n",p.x,p.y);
        setting_msg_clicked_event();
        break;
    }
    case LV_EVENT_PRESSED:
    {
        debug_d("p=%d:%d\n",p.x,p.y);
        break;
    }
    case LV_EVENT_PRESSING:
    {
        debug_d("ps=%d:%d\n",p.x,p.y);
        break;
    }
    case LV_EVENT_PRESS_LOST:
    {
        debug_d("pl=%d:%d\n",p.x,p.y);
        break;
    }   
    case LV_EVENT_RELEASED:
    {
        debug_d("pr=%d:%d\n",p.x,p.y);
        break;
    }
    default:break;
    }
}

static int setting_voice_clicked_event(void)
{
    ui_set_p.is_voice = !ui_set_p.is_voice;

    lv_img_set_src(ui_set_p.img_voice, pt_fs_get(icon_voice[ui_set_p.is_voice]));

    return 0;
}

static void setting_voice_event_callback(lv_event_t* event)
{
    if (event->code == LV_EVENT_CLICKED) {

        debug_d("voice clicked\n");
        setting_voice_clicked_event();
    }
}

static int setting_sleep_clicked_event(void)
{
    ui_set_p.is_sleep = !ui_set_p.is_sleep;

    lv_img_set_src(ui_set_p.img_sleep, pt_fs_get(icon_sleep[ui_set_p.is_sleep]));

    return 0;
}

static void setting_sleep_event_callback(lv_event_t* event)
{
    if (event->code == LV_EVENT_CLICKED) {

        debug_d("sleep clicked\n");

        setting_sleep_clicked_event();
    }
}

static int setting_bri_clicked_event(void)
{
    ui_set_p.is_auto_bri = !ui_set_p.is_auto_bri;
    set_auto_brightness(ui_set_p.is_auto_bri);

    lv_img_set_src(ui_set_p.img_bri, pt_fs_get(icon_bri[ui_set_p.is_auto_bri]));

    return 0;
}


static void setting_test_event_callback(lv_event_t* event)
{
    lv_point_t  p;
    lv_indev_t* indev  = NULL;
    indev = lv_indev_get_act();
    if(!indev){
        return -1;
    }

    lv_indev_get_point(indev, &p);

    switch(event->code){
    case LV_EVENT_CLICKED:
    {
        debug_d("c=%d:%d\n",p.x,p.y);
        break;
    }
    case LV_EVENT_PRESSED:
    {
        debug_d("p=%d:%d\n",p.x,p.y);
        break;
    }
    case LV_EVENT_PRESSING:
    {
        debug_d("ps=%d:%d\n",p.x,p.y);
        break;
    }
    case LV_EVENT_PRESS_LOST:
    {
        debug_d("pl=%d:%d\n",p.x,p.y);
        break;
    }   
    case LV_EVENT_RELEASED:
    {
        debug_d("pr=%d:%d\n",p.x,p.y);
        break;
    }
    default:break;
    }
}

static void setting_bri_event_callback(lv_event_t* event)
{
    if (event->code == LV_EVENT_CLICKED) {

        debug_d("bri clicked\n");
        setting_bri_clicked_event();
    }
}



static void setting_bright_event_callback(lv_event_t* event)
{
    lv_area_t   bar_area;
    lv_point_t  lv_point;
    lv_indev_t* indev = NULL;

    int w = 0;
    int h = 0;
    int t_h = 0;
    int vol = 0;

    indev = lv_indev_get_act();
    if(!indev){
        return ;
    }

    lv_event_stop_bubbling(event);

    lv_dir_t dir = lv_indev_get_scroll_dir(indev);
    if(dir == 3){
        return ;
    }

    // debug_d("dir=%d\n",dir);

    lv_indev_get_point(indev, &lv_point);
    lv_obj_get_coords(ui_set_p.bar_bri, &bar_area);

    w   = bar_area.x2 - bar_area.x1 + 1;
    h   = bar_area.y2 - bar_area.y1 + 1;
    t_h = bar_area.y2 - lv_point.y + 1;

    if(t_h < 0)
        t_h = 0;
    if(t_h > h)
        t_h = h;

    switch(event->code){
    case LV_EVENT_PRESSED:
    {
        debug_d("pr\n");
        root_gesture_disable();
        break;
    }
    case LV_EVENT_RELEASED:
    {
        debug_d("re\n");
        root_gesture_enable();
        break;
    }
    case LV_EVENT_CLICKED:
    {
        debug_d("c\n");
        break;
    }
    case LV_EVENT_PRESSING:
    {
        debug_d("ps\n");
        ui_set_p.is_auto_bri = 0;
        set_auto_brightness(ui_set_p.is_auto_bri);

        lv_img_set_src(ui_set_p.img_bri, pt_fs_get(icon_bri[ui_set_p.is_auto_bri]));

        vol = t_h*pt_bright_get_max()/h;
        pt_bright_set(vol);
        lv_bar_set_value(ui_set_p.bar_bri,pt_bright_get(), LV_ANIM_ON);

        // debug_d("bri=%d\n",vol);
        break;
    }
    default:break;
    }
}

//自动亮度调节时，自动更新亮度条
void timer_auto_light(void)
{
    if(!ui_set_p.is_auto_bri)
        return;

    ui_set_p.target_light = pt_bright_get();
    
    if(ui_set_p.current_light == ui_set_p.target_light)
    {
        return;
    }    
    else if(ui_set_p.current_light > ui_set_p.target_light)
    {
        ui_set_p.current_light--;
        lv_bar_set_value(ui_set_p.bar_bri,ui_set_p.current_light, LV_ANIM_ON);
    }
    else if(ui_set_p.current_light < ui_set_p.target_light)
    {
        ui_set_p.current_light++;
        lv_bar_set_value(ui_set_p.bar_bri,ui_set_p.current_light, LV_ANIM_ON);
    }
}

static void setting_volume_event_callback(lv_event_t* event)
{
    lv_area_t   bar_area;
    lv_point_t  lv_point;
    lv_indev_t* indev = NULL;

    int w = 0;
    int h = 0;
    int t_h = 0;
    int vol = 0;

    indev = lv_indev_get_act();
    if(!indev){
        return ;
    }

    lv_indev_get_point(indev, &lv_point);
    lv_obj_get_coords(ui_set_p.bar_vol, &bar_area);

    w   = bar_area.x2 - bar_area.x1 + 1;
    h   = bar_area.y2 - bar_area.y1 + 1;
    t_h = bar_area.y2 - lv_point.y + 1;

    if(t_h < 0)
        t_h = 0;
    if(t_h > h)
        t_h = h;

    switch(event->code){
    case LV_EVENT_PRESSED:
    {
        root_gesture_disable();
        break;
    }
    case LV_EVENT_RELEASED:
    {
        root_gesture_enable();
        break;
    }
    case LV_EVENT_CLICKED:
    {

        break;
    }
    case LV_EVENT_PRESSING:
    {
        vol = t_h*pt_volume_get_max()/h;
        pt_volume_set(vol);
        lv_bar_set_value(ui_set_p.bar_vol,pt_volume_get(), LV_ANIM_ON);

        debug_d("vol=%d\n",vol);
        break;
    }
    default:break;
    }
}

void ui_init_setting_page_create(lv_obj_t* parent,void* ui_data)
{
    lv_obj_t *superview = parent;
    
    char txt_buff[24];

    ui_set_p.msg_num     = msg_info_get_noread();
    ui_set_p.is_msg      = 0;
    ui_set_p.is_voice    = 0;
    ui_set_p.is_sleep    = 0;
    ui_set_p.is_auto_bri = get_auto_brightness_status();

    lv_coord_t pw = 0;
    lv_coord_t ph = 0;

    pw = lv_disp_get_hor_res(NULL);
    ph = lv_disp_get_ver_res(NULL);
    //**************************************************************************************
    ui_set_p.bg  = lv_obj_create(superview);

    lv_obj_set_style_radius(ui_set_p.bg, 0, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_p.bg, 0, LV_PART_MAIN);
    lv_obj_set_style_border_opa(ui_set_p.bg, LV_OPA_0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_p.bg, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
    lv_obj_set_style_pad_all(ui_set_p.bg,0,0);
    lv_obj_set_scrollbar_mode(ui_set_p.bg, LV_SCROLLBAR_MODE_OFF);

    #if 0
	lv_obj_set_style_bg_opa(ui_set_p.bg, LV_OPA_0, LV_PART_MAIN);
    #else
	lv_obj_set_style_bg_opa(ui_set_p.bg, LV_OPA_100, LV_PART_MAIN);
	lv_obj_set_style_bg_color(ui_set_p.bg, lv_color_make(0,0,0), LV_PART_MAIN);
    #endif

	lv_obj_set_pos(ui_set_p.bg,0,0);
	lv_obj_set_size(ui_set_p.bg,pw,ph);
    //****************************************************** */
    ui_set_p.img_point1 = lv_img_create(superview);
    lv_img_set_src(ui_set_p.img_point1,pt_fs_get(icon_point_setting[1]));
    lv_obj_set_pos(ui_set_p.img_point1,221,19);

    ui_set_p.img_point2 = lv_img_create(superview);
    lv_img_set_src(ui_set_p.img_point2,pt_fs_get(icon_point_setting[0]));
    lv_obj_set_pos(ui_set_p.img_point2,235,19);

    //**************************************************************************************
    //msg status

    ui_set_p.bg_msg = lv_obj_create(superview);

    lv_obj_set_pos(ui_set_p.bg_msg,25,60);
    lv_obj_set_size(ui_set_p.bg_msg,70,70);

	lv_obj_set_style_radius(ui_set_p.bg_msg, 0, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_p.bg_msg, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_p.bg_msg, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui_set_p.bg_msg, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui_set_p.bg_msg, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui_set_p.bg_msg, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(ui_set_p.bg_msg, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui_set_p.bg_msg, 0,0);


    #if 1
    lv_obj_set_style_bg_opa(ui_set_p.bg_msg,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_set_p.bg_msg,LV_OPA_100,0);
    lv_obj_set_style_bg_color(ui_set_p.bg_msg,lv_color_make(0xff,0,0),0);
    #endif

    lv_obj_add_event_cb(ui_set_p.bg_msg, setting_msg_event_callback, LV_EVENT_ALL, NULL);

    ui_set_p.img_msg = lv_img_create(ui_set_p.bg_msg);
    lv_img_set_src(ui_set_p.img_msg, pt_fs_get(icon_msg[ui_set_p.is_msg]));
    lv_obj_set_pos(ui_set_p.img_msg,0,0);
    //**************************************************************************************
    //msg num

    ui_set_p.bg_msg_num = lv_obj_create(superview);

    lv_obj_set_pos(ui_set_p.bg_msg_num,66,56);
    lv_obj_set_size(ui_set_p.bg_msg_num,34,20);

	lv_obj_set_style_radius(ui_set_p.bg_msg_num, 10, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_p.bg_msg_num, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_p.bg_msg_num, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui_set_p.bg_msg_num, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui_set_p.bg_msg_num, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui_set_p.bg_msg_num, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(ui_set_p.bg_msg_num, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui_set_p.bg_msg_num, 0,0);

    #if 0
    lv_obj_set_style_bg_opa(ui_set_p.bg_msg_num,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_set_p.bg_msg_num,LV_OPA_100,0);
    lv_obj_set_style_bg_color(ui_set_p.bg_msg_num,lv_color_make(0xff,0x3b,0x30),0);
    #endif

    ui_set_p.lab_msg_num = lv_label_create(ui_set_p.bg_msg_num);
    lv_obj_set_pos(ui_set_p.lab_msg_num,4,-2);
    lv_obj_set_size(ui_set_p.lab_msg_num,26,22);
    lv_obj_set_style_text_align(ui_set_p.lab_msg_num,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_set_p.lab_msg_num,LV_OPA_100,0);
    lv_obj_set_style_text_color(ui_set_p.lab_msg_num,lv_color_make(0xff,0xff,0xff),0);
    lv_obj_set_style_text_font(ui_set_p.lab_msg_num, lv_font_get(18), LV_STATE_DEFAULT);

    memset(txt_buff,0,sizeof(txt_buff));
    snprintf(txt_buff,24,"%d", ui_set_p.msg_num);
    lv_label_set_text( ui_set_p.lab_msg_num, txt_buff);

    if(ui_set_p.msg_num){
        if(ui_set_p.bg_msg_num && lv_obj_has_flag(ui_set_p.bg_msg_num,LV_OBJ_FLAG_HIDDEN)){
            lv_obj_clear_flag(ui_set_p.bg_msg_num,LV_OBJ_FLAG_HIDDEN);
        }
    } else {
        if(ui_set_p.bg_msg_num){
            lv_obj_add_flag(ui_set_p.bg_msg_num,LV_OBJ_FLAG_HIDDEN);
        }
    }
    //**************************************************************************************
    //voice status

    ui_set_p.bg_voice = lv_obj_create(superview);

    lv_obj_set_pos(ui_set_p.bg_voice,136,60);
    lv_obj_set_size(ui_set_p.bg_voice,70,70);

	lv_obj_set_style_radius(ui_set_p.bg_voice, 0, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_p.bg_voice, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_p.bg_voice, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui_set_p.bg_voice, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui_set_p.bg_voice, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui_set_p.bg_voice, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(ui_set_p.bg_voice, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui_set_p.bg_voice, 0,0);


    #if 1
    lv_obj_set_style_bg_opa(ui_set_p.bg_voice,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_set_p.bg_voice,LV_OPA_100,0);
    lv_obj_set_style_bg_color(ui_set_p.bg_voice,lv_color_make(0xff,0,0),0);
    #endif

    lv_obj_add_event_cb(ui_set_p.bg_voice, setting_voice_event_callback, LV_EVENT_ALL, NULL);

    ui_set_p.img_voice = lv_img_create(ui_set_p.bg_voice);
    lv_img_set_src(ui_set_p.img_voice, pt_fs_get(icon_voice[ui_set_p.is_voice]));
    lv_obj_set_pos(ui_set_p.img_voice,0,0);
    //**************************************************************************************
    //sleep status

    ui_set_p.bg_sleep = lv_obj_create(superview);

    lv_obj_set_pos(ui_set_p.bg_sleep,25,190);
    lv_obj_set_size(ui_set_p.bg_sleep,70,70);

	lv_obj_set_style_radius(ui_set_p.bg_sleep, 0, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_p.bg_sleep, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_p.bg_sleep, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui_set_p.bg_sleep, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui_set_p.bg_sleep, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui_set_p.bg_sleep, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(ui_set_p.bg_sleep, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui_set_p.bg_sleep, 0,0);

    #if 1
    lv_obj_set_style_bg_opa(ui_set_p.bg_sleep,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_set_p.bg_sleep,LV_OPA_100,0);
    lv_obj_set_style_bg_color(ui_set_p.bg_sleep,lv_color_make(0xff,0,0),0);
    #endif

    lv_obj_add_event_cb(ui_set_p.bg_sleep, setting_sleep_event_callback, LV_EVENT_ALL, NULL);

    ui_set_p.img_sleep = lv_img_create(ui_set_p.bg_sleep);
    lv_img_set_src(ui_set_p.img_sleep, pt_fs_get(icon_sleep[ui_set_p.is_sleep]));
    lv_obj_set_pos(ui_set_p.img_sleep,0,0);
    //**************************************************************************************
    //birght status

    ui_set_p.bg_bri = lv_obj_create(superview);

    lv_obj_set_pos(ui_set_p.bg_bri,136,190);
    lv_obj_set_size(ui_set_p.bg_bri,70,70);

	lv_obj_set_style_radius(ui_set_p.bg_bri, 0, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_p.bg_bri, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_p.bg_bri, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui_set_p.bg_bri, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui_set_p.bg_bri, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui_set_p.bg_bri, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(ui_set_p.bg_bri, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui_set_p.bg_bri, 0,0);

    #if 1
    lv_obj_set_style_bg_opa(ui_set_p.bg_bri,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_set_p.bg_bri,LV_OPA_100,0);
    lv_obj_set_style_bg_color(ui_set_p.bg_bri,lv_color_make(0xff,0,0),0);
    #endif

    lv_obj_add_event_cb(ui_set_p.bg_bri, setting_bri_event_callback, LV_EVENT_ALL, NULL);

    ui_set_p.img_bri = lv_img_create(ui_set_p.bg_bri);
    lv_img_set_src(ui_set_p.img_bri, pt_fs_get(icon_bri[ui_set_p.is_auto_bri]));
    lv_obj_set_pos(ui_set_p.img_bri,0,0);

    ui_set_p.auto_light_timer = lv_timer_create(timer_auto_light,50,NULL);
    //**************************************************************************************
    //icon name

    ui_set_p.lab_msg = lv_label_create(superview);
    lv_obj_set_pos(ui_set_p.lab_msg,20,140);
    lv_obj_set_size(ui_set_p.lab_msg,80,22);
    lv_obj_set_style_text_align(ui_set_p.lab_msg,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_set_p.lab_msg,LV_OPA_60,0);
    lv_obj_set_style_text_color(ui_set_p.lab_msg,lv_color_make(0xff,0xff,0xff),0);
    lv_obj_set_style_text_font(ui_set_p.lab_msg, lv_font_get(18), LV_STATE_DEFAULT);
    lv_label_set_text( ui_set_p.lab_msg, "消息");

    ui_set_p.lab_voice = lv_label_create(superview);
    lv_obj_set_pos(ui_set_p.lab_voice,131,140);
    lv_obj_set_size(ui_set_p.lab_voice,80,22);
    lv_obj_set_style_text_align(ui_set_p.lab_voice,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_set_p.lab_voice,LV_OPA_60,0);
    lv_obj_set_style_text_color(ui_set_p.lab_voice,lv_color_make(0xff,0xff,0xff),0);
    lv_obj_set_style_text_font(ui_set_p.lab_voice, lv_font_get(18), LV_STATE_DEFAULT);
    lv_label_set_text( ui_set_p.lab_voice, "语音权限");

    ui_set_p.lab_sleep = lv_label_create(superview);
    lv_obj_set_pos(ui_set_p.lab_sleep,20,270);
    lv_obj_set_size(ui_set_p.lab_sleep,80,22);
    lv_obj_set_style_text_align(ui_set_p.lab_sleep,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_set_p.lab_sleep,LV_OPA_60,0);
    lv_obj_set_style_text_color(ui_set_p.lab_sleep,lv_color_make(0xff,0xff,0xff),0);
    lv_obj_set_style_text_font(ui_set_p.lab_sleep, lv_font_get(18), LV_STATE_DEFAULT);
    lv_label_set_text( ui_set_p.lab_sleep, "睡眠模式");

    ui_set_p.lab_bri = lv_label_create(superview);
    lv_obj_set_pos(ui_set_p.lab_bri,131,270);
    lv_obj_set_size(ui_set_p.lab_bri,80,22);
    lv_obj_set_style_text_align(ui_set_p.lab_bri,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_set_p.lab_bri,LV_OPA_60,0);
    lv_obj_set_style_text_color(ui_set_p.lab_bri,lv_color_make(0xff,0xff,0xff),0);
    lv_obj_set_style_text_font(ui_set_p.lab_bri, lv_font_get(18), LV_STATE_DEFAULT);
    lv_label_set_text( ui_set_p.lab_bri, "自动亮度");
    //*************************************************************************************
    //bri bar

	ui_set_p.bar_bri = lv_bar_create(superview);

    lv_obj_set_style_radius(ui_set_p.bar_bri, 40, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_p.bar_bri, 0, LV_PART_MAIN);
    lv_obj_set_style_border_opa(ui_set_p.bar_bri, LV_OPA_0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_p.bar_bri, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);

	lv_obj_set_pos(ui_set_p.bar_bri,265,65);
	lv_obj_set_size(ui_set_p.bar_bri,70,222);
	lv_bar_set_range(ui_set_p.bar_bri,pt_bright_get_min(),pt_bright_get_max());
	lv_bar_set_mode(ui_set_p.bar_bri, LV_BAR_MODE_SYMMETRICAL);

#if 0
	lv_obj_set_style_bg_opa(ui_set_p.bar_bri, LV_OPA_0, LV_PART_MAIN);
#else
	lv_obj_set_style_bg_opa(ui_set_p.bar_bri, LV_OPA_10, LV_PART_MAIN);
	lv_obj_set_style_bg_color(ui_set_p.bar_bri, lv_color_make(0xFF, 0xFF, 0xFF), LV_PART_MAIN);
#endif

	lv_obj_set_style_bg_opa(ui_set_p.bar_bri, LV_OPA_100, LV_PART_INDICATOR);
	lv_obj_set_style_bg_color(ui_set_p.bar_bri, lv_color_make(0xc7, 0xc7, 0xcc), LV_PART_INDICATOR);

    ui_set_p.current_light = pt_bright_get();
	lv_bar_set_value(ui_set_p.bar_bri,ui_set_p.current_light, LV_ANIM_ON);

    lv_obj_clear_flag(ui_set_p.bar_bri,LV_OBJ_FLAG_GESTURE_BUBBLE);
    lv_obj_add_event_cb(ui_set_p.bar_bri, setting_bright_event_callback, LV_EVENT_ALL, NULL);
    //***************************************************************

    ui_set_p.bar_bri_img = lv_img_create(superview);
    lv_img_set_src(ui_set_p.bar_bri_img, pt_fs_get(icon_bright[0]));
    lv_obj_set_pos(ui_set_p.bar_bri_img,276,235);
    //*************************************************************************************
    //volume bar

	ui_set_p.bar_vol = lv_bar_create(superview);

    lv_obj_set_style_radius(ui_set_p.bar_vol, 40, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_p.bar_vol, 0, LV_PART_MAIN);
    lv_obj_set_style_border_opa(ui_set_p.bar_vol, LV_OPA_0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_p.bar_vol, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);

	lv_obj_set_pos(ui_set_p.bar_vol,361,65);
	lv_obj_set_size(ui_set_p.bar_vol,70,222);
	lv_bar_set_range(ui_set_p.bar_vol,pt_volume_get_min(),pt_volume_get_max());
	lv_bar_set_mode(ui_set_p.bar_vol, LV_BAR_MODE_SYMMETRICAL);

#if 0
	lv_obj_set_style_bg_opa(ui_set_p.bar_vol, LV_OPA_0, LV_PART_MAIN);
#else
	lv_obj_set_style_bg_opa(ui_set_p.bar_vol, LV_OPA_10, LV_PART_MAIN);
	lv_obj_set_style_bg_color(ui_set_p.bar_vol, lv_color_make(0xFF, 0xFF, 0xFF), LV_PART_MAIN);
#endif

	lv_obj_set_style_bg_opa(ui_set_p.bar_vol, LV_OPA_100, LV_PART_INDICATOR);
	lv_obj_set_style_bg_color(ui_set_p.bar_vol, lv_color_make(0xc7, 0xc7, 0xcc), LV_PART_INDICATOR);

	lv_bar_set_value(ui_set_p.bar_vol,pt_volume_get(), LV_ANIM_ON);

    lv_obj_clear_flag(ui_set_p.bar_vol,LV_OBJ_FLAG_GESTURE_BUBBLE);
    lv_obj_add_event_cb(ui_set_p.bar_vol, setting_volume_event_callback, LV_EVENT_ALL, NULL);
    //***************************************************************

    ui_set_p.bar_vol_img = lv_img_create(superview);
    lv_img_set_src(ui_set_p.bar_vol_img, pt_fs_get(icon_volume[0]));
    lv_obj_set_pos(ui_set_p.bar_vol_img,375,235);
    //*************************************************************************************

    return ;
}

void ui_init_setting_page_destroy(void* ui_data)
{
    ui_set_p.msg_num     = 0;
    ui_set_p.is_msg      = 0;
    ui_set_p.is_voice    = 0;
    ui_set_p.is_sleep    = 0;
    set_auto_brightness(ui_set_p.is_auto_bri);
    ui_set_p.is_auto_bri = 0;

    if(!ui_set_p.bg)
        return ;

    if(ui_set_p.bg){
        lv_obj_del(ui_set_p.bg);
        ui_set_p.bg = NULL;
    }
    if(ui_set_p.img_point1){
        lv_obj_del(ui_set_p.img_point1);
        ui_set_p.img_point1 = NULL;
    }
    if(ui_set_p.img_point2){
        lv_obj_del(ui_set_p.img_point2);
        ui_set_p.img_point2 = NULL;
    }
    if(ui_set_p.bg_msg){
        lv_obj_del(ui_set_p.bg_msg);
        ui_set_p.bg_msg = NULL;
        ui_set_p.img_msg = NULL;
    }
    if(ui_set_p.bg_msg_num){
        lv_obj_del(ui_set_p.bg_msg_num);
        ui_set_p.bg_msg_num = NULL;
        ui_set_p.lab_msg_num = NULL;
    }
    if(ui_set_p.bg_voice){
        lv_obj_del(ui_set_p.bg_voice);
        ui_set_p.bg_voice = NULL;
        ui_set_p.img_voice = NULL;
    }
    if(ui_set_p.bg_sleep){
        lv_obj_del(ui_set_p.bg_sleep);
        ui_set_p.bg_sleep = NULL;
        ui_set_p.img_sleep = NULL;
    }
    if(ui_set_p.bg_bri){
        lv_obj_del(ui_set_p.bg_bri);
        ui_set_p.bg_bri = NULL;
        ui_set_p.img_bri = NULL;
    }
    if(ui_set_p.lab_msg){
        lv_obj_del(ui_set_p.lab_msg);
        ui_set_p.lab_msg = NULL;
    }
    if(ui_set_p.lab_voice){
        lv_obj_del(ui_set_p.lab_voice);
        ui_set_p.lab_voice = NULL;
    }
    if(ui_set_p.lab_sleep){
        lv_obj_del(ui_set_p.lab_sleep);
        ui_set_p.lab_sleep = NULL;
    }
    if(ui_set_p.lab_bri){
        lv_obj_del(ui_set_p.lab_bri);
        ui_set_p.lab_bri = NULL;
    }
    if(ui_set_p.bar_bri){
        lv_obj_del(ui_set_p.bar_bri);
        ui_set_p.bar_bri = NULL;
    }
    if(ui_set_p.bar_bri_img){
        lv_obj_del(ui_set_p.bar_bri_img);
        ui_set_p.bar_bri_img = NULL;
    }
    if(ui_set_p.bar_vol){
        lv_obj_del(ui_set_p.bar_vol);
        ui_set_p.bar_vol = NULL;
    }
    if(ui_set_p.bar_vol_img){
        lv_obj_del(ui_set_p.bar_vol_img);
        ui_set_p.bar_vol_img = NULL;
    }
    if(ui_set_p.touch_timer){
        lv_timer_del(ui_set_p.touch_timer);
        ui_set_p.touch_timer = NULL;
    }
    return ;
}

static void setting_page_on_view_did_load(page_base_t* self)
{}
static setting_page_on_custom_attr_config(page_base_t* self)
{}
static setting_page_on_view_load(page_base_t* self)
{
    debug_d("create\n");
    lv_obj_add_flag(self->root,LV_OBJ_FLAG_HIDDEN);

    setting_page_create();
    setting_page_show();
}
static setting_page_on_view_will_appear(page_base_t* self)
{}
static setting_page_on_view_did_appear(page_base_t* self)
{}
static setting_page_on_view_will_disappear(page_base_t* self)
{}
static setting_page_on_view_did_disappear(page_base_t* self)
{}
static void setting_page_on_base_view_did_unload(page_base_t* self)
{
    debug_d("destroy\n");
    setting_page_hide();
}

static page_vtable_t page_param = {
    .on_custom_attr_config  = setting_page_on_custom_attr_config,
    .on_view_load           = setting_page_on_view_load,
    .on_view_did_load       = setting_page_on_view_did_load,
    .on_view_will_appear    = setting_page_on_view_will_appear,
    .on_view_did_appear     = setting_page_on_view_did_appear,
    .on_view_will_disappear = setting_page_on_view_will_disappear,
    .on_view_did_disappear  = setting_page_on_view_did_disappear,
    .on_view_did_unload     = setting_page_on_base_view_did_unload,
};


page_vtable_t* lv_page_setting_create(void)
{
    return &page_param;
}

static void settting_on_page_create(lv_obj_t* parent,void* ui_data)
{
    debug_d("create\n");
    ui_init_setting_page_create(parent,ui_data);
}   
static void settting_on_page_destroy(void* ui_data)
{
    debug_d("destroy\n");
    ui_init_setting_page_destroy(ui_data);
}
static void settting_on_page_update(void* ui_data)
{

}
static page_method_t page_settting = {
    .on_page_create  = settting_on_page_create,
    .on_page_destroy = settting_on_page_destroy,
    .on_page_update  = settting_on_page_update,
};

page_method_t* lv_settting_page_method(void)
{
    return &page_settting;
}

static void setting_page_scroll_timer_callback(struct _lv_timer_t * t)
{
    lv_coord_t     x_end = 0;
    lv_coord_t     index = 0;
    lv_coord_t     pw    = lv_disp_get_hor_res(NULL);
    lv_coord_t     pt    = midea_collection_get_data_count(THE_SETTING);

    debug_d("scrend\n");

    if(ui_set_p.touch_timer){
        lv_timer_del(ui_set_p.touch_timer);
        ui_set_p.touch_timer = NULL;
    }

    lv_coord_t scroll_x = lv_obj_get_scroll_x(ui_set_p.setting_collection);


    debug_d("scroll_x=%d\n",scroll_x);

    float scroll_ratio = (float)abs(scroll_x) /pw;

    debug_d("scroll_ratio=%f\n",scroll_ratio);

    if(scroll_ratio > 0.25f){

    }

    return ;

    x_end = lv_obj_get_scroll_x(ui_set_p.setting_collection);
    if(x_end < 0){
        return ;
    }
    if(x_end > (pt*pw)){
        return ;
    }

    index = (x_end + pw / 2) / pw;

    // debug_d("scroll end,x_end=%d:%d\n",index,x_end);
    scroll_collection_view_to_index_anim(THE_SETTING,index,0);


}

static void setting_page_collection_view_callback(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *obj        = lv_event_get_target(e);
    lv_coord_t        pw = lv_disp_get_hor_res(NULL);

    lv_indev_t* indev = lv_indev_get_act();
    if(!indev){
        return ;
    }

    switch(code){
    case LV_EVENT_SCROLL_BEGIN:
    {
        break;
    }
    case LV_EVENT_SCROLL_END:
    {
        if(!ui_set_p.touch_timer){
            ui_set_p.touch_timer = lv_timer_create(setting_page_scroll_timer_callback,60,NULL);
        }else{
            lv_timer_reset(ui_set_p.touch_timer);
        }
        break;
    }
    default:break;
    }
}
void setting_pgae_update_collection(void *data, lv_obj_t *item_view, int32_t index, uint8_t ui_type)
{
    debug_d("update\n");

    

}

void setting_page_install_collection(void *data, lv_obj_t* item_view, int32_t index, uint8_t ui_type)
{
    debug_d("install-index=%d\n",index);
    page_info_t* page = NULL;
    if(!data){
        return -1;
    }

    page = (page_info_t*)data;
    page->method->on_page_create(item_view,page->data);

    return ;
}

void setting_page_free_data(void *data)
{
    debug_d("free\n");

    page_info_t* page = NULL;
    if(!data){
        return -1;
    }

    page = (page_info_t*)data;
    page->method->on_page_destroy(page->data);
}

static void setting_page_show_callback(lv_anim_t *a)
{
    lv_obj_clear_flag(ui_set_p.setting_page,LV_OBJ_FLAG_HIDDEN);
}

int setting_page_show(void)
{
    lv_coord_t ph = lv_disp_get_ver_res(NULL);

    if(ui_set_p.setting_page){
        
        lv_obj_clear_flag(ui_set_p.setting_page,LV_OBJ_FLAG_HIDDEN);

        lv_obj_set_y(ui_set_p.setting_page,-ph);

        lv_anim_t a;
        lv_anim_init(&a);
        lv_anim_set_var(&a,ui_set_p.setting_page);
        lv_anim_set_values(&a, -ph, 0);
        lv_anim_set_time(&a, 400);
        lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_y);
        lv_anim_set_path_cb(&a, lv_anim_path_ease_in);
        lv_anim_set_ready_cb(&a, (lv_anim_exec_xcb_t)setting_page_show_callback);
        lv_anim_start(&a);
    }
    return 0;
}


static void setting_page_hide_callback(lv_anim_t *a)
{
    lv_obj_add_flag(ui_set_p.setting_page,LV_OBJ_FLAG_HIDDEN);
}

int setting_page_hide(void)
{
    lv_coord_t ph = lv_disp_get_ver_res(NULL);

    if(!ui_set_p.setting_page){
        return -1;
    }

    // lv_obj_set_y(ui_set_p.setting_page,0); // 从屏幕底部开始

    // // 创建向上滑动的动画
    // lv_anim_t a;
    // lv_anim_init(&a);
    // lv_anim_set_var(&a, ui_set_p.setting_page);
    // lv_anim_set_values(&a,0, -ph); // 从屏幕底部到顶部
    // lv_anim_set_time(&a, 300); // 动画时长400毫秒
    // lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_y); // 修改Y坐标
    // lv_anim_set_path_cb(&a, lv_anim_path_ease_out);
    // lv_anim_set_ready_cb(&a, (lv_anim_exec_xcb_t)setting_page_hide_callback);
    // lv_anim_start(&a);

    lv_obj_set_x(ui_set_p.setting_page,0); // 从屏幕底部开始

    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_var(&a, ui_set_p.setting_page);
    lv_anim_set_values(&a,0, -2*ph);
    lv_anim_set_time(&a, 300); 
    lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_x); 
    lv_anim_set_path_cb(&a, lv_anim_path_ease_out);
    lv_anim_set_ready_cb(&a, (lv_anim_exec_xcb_t)setting_page_hide_callback);
    lv_anim_start(&a);

  
    return 0;
}

static void setting_page_gesture_cb(lv_event_t* e)
{
    lv_point_t vect;
    lv_obj_t *obj = NULL;
    lv_indev_t* indev = NULL;
    lv_event_code_t code = 0;
    lv_dir_t dir = LV_DIR_NONE;

    code  = lv_event_get_code(e);
    if(code != LV_EVENT_GESTURE)
        return ;

    dir = lv_indev_get_gesture_dir(lv_indev_get_act());
    switch(dir){
    case LV_DIR_TOP:
    {
        root_gesture_top();
        break;
    }
    default:break;}
}

int setting_page_create(void)
{
    lv_coord_t pw = 0;
    lv_coord_t ph = 0;

    pw = lv_disp_get_hor_res(NULL);
    ph = lv_disp_get_ver_res(NULL);

    if(ui_set_p.setting_page)
        return 0;

    ui_set_p.setting_page = lv_obj_create(lv_scr_act());

    lv_obj_set_pos(ui_set_p.setting_page,0,0);
    lv_obj_set_width(ui_set_p.setting_page, pw);
    lv_obj_set_height(ui_set_p.setting_page, ph);

    lv_obj_set_style_radius(ui_set_p.setting_page, 0, 0);
    lv_obj_set_style_border_width(ui_set_p.setting_page, 0, 0);
    lv_obj_set_style_bg_color(ui_set_p.setting_page, lv_color_hex(0xe8380d), 0);
    lv_obj_set_style_bg_opa(ui_set_p.setting_page, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_all(ui_set_p.setting_page, 0, 0);
    
    lv_obj_clear_flag(ui_set_p.setting_page,LV_OBJ_FLAG_SCROLLABLE);

    #if 1
	lv_obj_set_style_bg_opa(ui_set_p.setting_page, LV_OPA_0, LV_PART_MAIN);
    #else
	lv_obj_set_style_bg_opa(ui_set_p.setting_page, LV_OPA_100, LV_PART_MAIN);
	lv_obj_set_style_bg_color(ui_set_p.setting_page, lv_color_make(0xff,0,0xff), LV_PART_MAIN);
    #endif

    // lv_obj_set_style_pad_row(ui_set_p.setting_page, 0, 0);
    // lv_obj_set_style_pad_column(ui_set_p.setting_page, 0, 0);

    lv_obj_clear_flag(ui_set_p.setting_page,LV_OBJ_FLAG_GESTURE_BUBBLE);
    lv_obj_add_event_cb(ui_set_p.setting_page,setting_page_gesture_cb,LV_EVENT_GESTURE,NULL);
    //************************************************
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_pad_bottom(&style, 0);
    lv_style_set_pad_top(&style, 0);
    lv_style_set_pad_left(&style, 0);
    lv_style_set_pad_right(&style, 0);
    lv_style_set_border_width(&style, 0);
    lv_style_set_bg_color(&style,lv_color_hex(0xff0000));
    lv_style_set_bg_opa(&style,LV_OPA_100);
    //************************************************
    ui_set_p.setting_collection = create_midea_collection(THE_SETTING,ui_set_p.setting_page,
        LV_DIR_HOR,
        setting_pgae_update_collection,
        setting_page_install_collection,
        setting_page_free_data);
    if(ui_set_p.setting_collection == NULL){
        debug_d("err0\n");
        return -1;
    }

    midea_collection_view* collection = (midea_collection_view*)ui_set_p.setting_collection;
    collection->dynamic_load_tag.load_tag = MIDEA_DYNAMIC_LOAD;

   

    lv_obj_add_style(ui_set_p.setting_collection, &style, LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_set_p.setting_collection,LV_OPA_0,LV_PART_MAIN);

    lv_obj_set_style_pad_row(ui_set_p.setting_collection, 0, 0);
    lv_obj_set_style_pad_column(ui_set_p.setting_collection, 0, 0);

    lv_obj_add_event_cb(ui_set_p.setting_collection,setting_page_collection_view_callback,LV_EVENT_ALL,NULL);
    //************************************************
    static page_info_t setting;

    setting.method = lv_settting_page_method();
    setting.data   = NULL;
    add_midea_collection_data(THE_SETTING,&setting,UI_TYPE_SETTING);
    //*********************************
    static page_info_t setting2;
    setting2.method  = lv_settting2_page_method();
    setting2.data    = NULL;
    add_midea_collection_data(THE_SETTING,&setting2,UI_TYPE_SETTING2);
    //************************************************
    update_midea_collection(THE_SETTING);
    //************************************************

    ui_set_p.index = 0;

    return 0;
}
